<template>
  <div class="systemInfo" id="systemInfo">
    <!-- 表格展示部分 -->
    <div class="table">
      <div class="data">
        <ul>
          <li class="left-li" v-for="(item, index) in headerData" :key="index">
            {{ item }}
          </li>
        </ul>
        <ul class="table-right">
          <li class="productName">
            <span>{{ tableData.productName }}</span>
            <span class="try_img" @click="goWebsite"
              ><img src="../../../assets/imgs/authorization.png" alt=""
            /></span>
          </li>
          <li>{{ tableData.productVersion }}</li>
          <li>{{ tableData.userCompanyName }}</li>
          <li>
            {{ tableData.copyright }}
            <a @click="openWeb()">{{ tableData.homePage }}</a>
          </li>
          <li>{{ tableData.osName }}</li>
          <li>{{ tableData.jdkVersion }}</li>
          <li>{{ tableData.dbType }}</li>
          <li>{{ tableData.dpAddress }}</li>
          <li>{{ tableData.dbPort }}</li>
          <li>{{ tableData.dbName }}</li>
        </ul>
      </div>
    </div>
    <base-modal
      :width="modAlProps.width"
      :isVisible="isVisible"
      :title="modAlProps.title"
      :buttons="buttons"
      cancleText="稍后处理"
      @handleOk="buttonMessage"
      @handleCancle="handleCancle"
      class="site_modal"
      v-if="isVisible"
    >
      <template v-slot:body>
        <div class="model">
          <div class="logo"><img src="../../../assets/imgs/tryIcon.png" alt="" /></div>
          <div class="info">
            <div class="title">JEECMS {{ versions }}</div>
            <div class="item">
              授权号:<span>{{ authorizationNumber }}</span>
            </div>
            <div class="item">到期时间: <span>{{deadline}}</span></div>
            <div class="item">购买授权请访问: <a @click="openWeb">www.jeecms.com</a></div>
          </div>
        </div>
        <div class="useType_container" v-if="showRenewal">
          <div class="tips">
            选择续期类型
            <a-tooltip>
              <template #title>
                <span>不同授权类型的时长及通过方式不一致，如下：限期授权：免审通过，第一次申请为{{ configData.trialRule.deadline[0] }}天；第二次申请为{{ configData.trialRule.deadline[1] }}天；第三次申请为{{ configData.trialRule.deadline[2] }}天。个人非商业用途：需通过平台审核，第一次申请为60天；第二次申请为30天；第三天申请为7天
注：一个授权号申请次数最多{{ configData.trialMaxCount }}次，申请次数为累加计算，不区分授权类型。</span>
              </template>
              <span style="color: #1f6ded" class="iconfont icon-hint1"></span>
            </a-tooltip>
          </div>
          <a-radio-group style="min-width:282px;margin-top: 10px" v-model:value="useType">
            <a-radio-button value="1" style="margin-right: 24px;width: 129px;text-align: center">限期授权</a-radio-button>
            <a-radio-button value="2">个人非商业用途</a-radio-button>
          </a-radio-group>
          <div>
            <a-input class="user_info" v-model:value="userName" placeholder="请输入联系人姓名" allowClear></a-input>
            <a-input class="user_info" v-model:value="userPhone" placeholder="请输入联系人电话" allowClear></a-input>
            <a-input v-if="useType === '2'" class="user_info" v-model:value="domain" placeholder="请输入网站域名，如baidu.com（必填）" allowClear></a-input>
            <a-input v-if="useType === '2'" class="user_info" v-model:value="siteName" placeholder="请输入网站名称（必填）" allowClear></a-input>
          </div>
          <a-button class="user_info" :disabled="useType == 1 ? (!userName || !userPhone) : (!userName || !userPhone || !domain || !siteName)" @click="handleApply">立即申请</a-button>
        </div>
      </template>
    </base-modal>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, computed, onMounted, nextTick, createVNode } from "vue";
import { useRoute, useRouter } from "vue-router";
import request from "@/api/index";
import { scrollType, modeProp, detailDataItemType } from "@/type.d";
import { message } from "ant-design-vue";
import baseModal from "@/components/modal/baseModal.vue";

// type
type configDataType = {
  records: any[];
};
interface dataSourceType {
  [propName: string]: any;
}
interface FormState {
  tagWord: dataSourceType;
  password: string;
  remember: boolean;
}

//data
//表格数据
const headerData = reactive<String[]>([
  "软件名称",
  "版本号",
  "用户单位",
  "版权所有",
  "操作系统",
  "JDK版本",
  "数据库类型",
  "数据库链接地址",
  "数据库端口",
  "数据库名称",
]);
const tableData = ref<dataSourceType>({});
// 模态框数据
const isVisible = ref<boolean>(false);
const modAlProps: modeProp = reactive({
  width: "M",
  title: "授权信息",
});
const buttons = [
  {
    text: "申请续期",
    "html-type": "submit",
  },
  {
    text: "前往购买",
    "html-type": "submit",
  },
];
const authorizationNumber = ref<any>("");
const versions = ref<any>("");
const useType = ref<any>("1");
const showRenewal = ref<boolean>(false);
const userName = ref<any>("");
const userPhone = ref<any>("");
const domain = ref<any>("");
const siteName = ref<any>("");
const configData = ref<any>({});
const lastUserInfo = ref<any>({});
const deadline = ref<any>("");

  
//methods
// 获取系统信息列表
const getTableData = () => {
  request
    .systemInfo()
    .then((res: any) => {
      tableData.value = res || {};
    })
    .catch((err: any) => {
      throw new Error(err);
    });
};
// 获取授权信息
const getLicenseData = () => {
  request
    .getLicense()
    .then((res: any) => {
      authorizationNumber.value = res.appid;
      versions.value = res.costPlanName;
      deadline.value = res.deadline;
    })
    .catch((err: any) => {
      throw new Error(err);
    });
};

const goWebsite = () => {
  getLicenseData();
  isVisible.value = true;
};

const buttonMessage = (msg: any) => {
  
  if(msg.text === '前往购买') {
    openWeb();
  }
  if(msg.text === '申请续期') {
    getConfigInfo();
    showRenewal.value = true;
  }
};

const getConfigInfo = async() => {
  const res = await request.getConf();
  const res2 = await request.getLastApplyInfo();
  configData.value = res;
  lastUserInfo.value = res2;
  userName.value = res2.contactName;
  userPhone.value = res2.contactTel;
  domain.value = res2.domain;
  siteName.value = res2.siteName;
};

const handleApply = () => {
  const params: any = {
    applyType: +useType.value,
    contactName: userName.value,
    contactTel: userPhone.value,
    domain: useType.value === '2' ? domain.value : null,
    siteName: useType.value === '2' ? siteName.value : null
  }
  request.authCertApply(params).then((res: any) => {
    if(res.code === "00000 00000") {
      message.success(res.message)
      isVisible.value = false;
      showRenewal.value = false;
      useType.value = "1";
      userName.value = "";
      userPhone.value = "";
      domain.value = "";
      siteName.value = "";
    }else {
      message.error(res.message)
    }
  }).catch((err: any) => {
    throw new Error(err)
  })
};

const handleCancle = () => {
  isVisible.value = false;
  showRenewal.value = false;
  useType.value = "1";
  userName.value = "";
  userPhone.value = "";
  domain.value = "";
  siteName.value = "";
};

// 跳转官网
const openWeb = () => {
  window.open(tableData.value.homePage);
};

//onmounted
onMounted(() => {
  getTableData();
});
</script>
<style lang="scss" scoped>
.systemInfo {
  width: 100%;
  height: calc(100% - 72px);
  padding: 16px;
  background-color: #fff;
  overflow-y: scroll;
  .table {
    width: 800px;
    height: 570px;
    border-radius: 2px;
    border: 1px solid rgba(0, 0, 0, 0.06);
    overflow: hidden;
    .data {
      display: flex;
      width: 100%;
      height: 100%;
      ul {
        width: 176px;
        overflow: hidden;
        li {
          font-size: 14px;
          padding-left: 23px;
          width: 100%;
          height: 57px;
          line-height: 56px;
          border-bottom: 1px solid rgba(0, 0, 0, 0.06);
          background-color: #ffffff;
          a {
            margin-left: 32px;
          }
          &:nth-last-child(1) {
            border-top: 0;
            border-bottom: 0;
          }
        }
        .left-li {
          padding-left: 0;
          padding-right: 24px;
          width: 176px;
          text-align: right;
          background-color: #fafafa;
          border-right: 1px solid rgba(0, 0, 0, 0.06);
        }
      }
      .table-right {
        width: 624px;
        .productName {
          display: flex;
          align-items: center;
          .try_img {
            cursor: pointer;
          }
          img {
            width: 26px;
            height: 25px;
            margin-left: 8px;
          }
        }
      }
    }
  }
}
.model {
  display: flex;
  align-items: center;
  .logo {
    width: 160px;
    height: 160px;
    margin-left: 11px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    margin-left: 24px;
    margin-top: 8px;
    .title {
     // width: 223px;
      height: 30px;
      font-size: 30px;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.85);
      line-height: 30px;
    }
    .item {
      margin-top: 20px;
      font-size: 14px;
      span {
        margin-left: 12px;
      }
      a {
        margin-left: 12px;
      }
    }
  }
}
.useType_container {
  padding-left: 195px;
  margin-top: 16px;
  .user_info {
    margin-top: 10px;
    width: 282px;
  }
}
</style>
